package com.ly.heart.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingPurchasePriceDetailEntity;
import com.ly.heart.domain.KingPurchasePriceEntity;
import com.ly.heart.mapper.KingPurchasePriceMapper;
import com.ly.heart.service.IKingPurchasePriceDetailService;
import com.ly.heart.service.IKingPurchasePriceService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 金蝶-采购价目/折扣主表Service业务层处理
 *
 * @author cxl
 * @date 2024-06-20
 */
@Service
public class KingPurchasePriceServiceImpl extends ServiceImpl<KingPurchasePriceMapper, KingPurchasePriceEntity> implements IKingPurchasePriceService {

    @Resource
    private KingPurchasePriceMapper kingPurchasePriceMapper;
    @Resource
    private IKingPurchasePriceDetailService kingPurchasePriceDetailService;

    /**
     * 查询金蝶-采购价目/折扣主表
     *
     * @param id 金蝶-采购价目/折扣主表ID
     * @return 金蝶-采购价目/折扣主表
     */
    @Override
    public KingPurchasePriceEntity selectKingPurchasePriceEntityById ( String id ) {
        KingPurchasePriceEntity kingPurchasePriceEntity = kingPurchasePriceMapper.selectKingPurchasePriceEntityById ( id );
//        KingPurchasePriceEntity kingPurchasePriceEntity = this.getById ( id );
//        List<KingPurchasePriceDetailEntity> detailList = kingPurchasePriceDetailService.list ( new QueryWrapper<KingPurchasePriceDetailEntity> ().eq ( "price_info_id", id ) );
        KingPurchasePriceDetailEntity purchasePriceDetailEntity = new KingPurchasePriceDetailEntity ();
        purchasePriceDetailEntity.setPriceInfoId ( id );
        List<KingPurchasePriceDetailEntity> detailList = kingPurchasePriceDetailService.selectKingPurchasePriceDetailEntityList ( purchasePriceDetailEntity );
        if ( ! CollectionUtils.isEmpty ( detailList ) ) {
            kingPurchasePriceEntity.setDetailList ( detailList );
        }
        return kingPurchasePriceEntity;
    }

    /**
     * 查询金蝶-采购价目/折扣主表列表
     *
     * @param kingPurchasePriceEntity 金蝶-采购价目/折扣主表
     * @return 金蝶-采购价目/折扣主表
     */
    @Override
    public AjaxResult selectKingPurchasePriceEntityList ( KingPurchasePriceEntity kingPurchasePriceEntity ) {
        Page<KingPurchasePriceEntity> page = this.page ( new Page<> ( kingPurchasePriceEntity.getPage (), kingPurchasePriceEntity.getLimit () ), new LambdaQueryWrapper<KingPurchasePriceEntity> ().eq ( KingPurchasePriceEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        List<KingPurchasePriceEntity> purchasePriceList = page.getRecords (); //kingPurchasePriceMapper.selectKingPurchasePriceEntityList ( kingPurchasePriceEntity );
        if ( ! CollectionUtils.isEmpty ( purchasePriceList ) ) {
            purchasePriceList.forEach ( purchasePrice -> {
                List<KingPurchasePriceDetailEntity> detailList = kingPurchasePriceDetailService.list ( new QueryWrapper<KingPurchasePriceDetailEntity> ().eq ( "price_info_id", purchasePrice.getId () ) );
                if ( ! CollectionUtils.isEmpty ( detailList ) ) {
                    kingPurchasePriceEntity.setDetailList ( detailList );
                }
            } );
        }
        return AjaxResult.success ( page );
    }

    /**
     * 新增金蝶-采购价目/折扣主表
     *
     * @param kingPurchasePriceEntity 金蝶-采购价目/折扣主表
     * @return 结果
     */
    @Override
    public int insertKingPurchasePriceEntity ( KingPurchasePriceEntity kingPurchasePriceEntity ) {
        String purchasePriceId = SnowflakeIdWorker.getInstance ().nextStringId ();
        String userId = SecurityUtils.getLoginUser ().getUser ().getUserId ();
        Date nowDate = DateUtils.getNowDate ();
        kingPurchasePriceEntity.setId ( purchasePriceId );
        kingPurchasePriceEntity.setUserId ( userId );
        kingPurchasePriceEntity.setCreateTime ( nowDate );
        if ( ! CollectionUtils.isEmpty ( kingPurchasePriceEntity.getDetailList () ) ) {
            kingPurchasePriceEntity.getDetailList ().forEach ( detail -> {
                detail.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                detail.setPriceInfoId ( purchasePriceId );
                detail.setUserId ( userId );
                detail.setCreateTime ( nowDate );
                kingPurchasePriceDetailService.save ( detail );
            } );
        }
        return kingPurchasePriceMapper.insertKingPurchasePriceEntity ( kingPurchasePriceEntity );
    }

    /**
     * 修改金蝶-采购价目/折扣主表
     *
     * @param kingPurchasePriceEntity 金蝶-采购价目/折扣主表
     * @return 结果
     */
    @Override
    public int updateKingPurchasePriceEntity ( KingPurchasePriceEntity kingPurchasePriceEntity ) {
        kingPurchasePriceDetailService.remove ( new QueryWrapper<KingPurchasePriceDetailEntity> ().eq ( "price_info_id", kingPurchasePriceEntity.getId () ) );
        if ( ! CollectionUtils.isEmpty ( kingPurchasePriceEntity.getDetailList () ) ) {
            kingPurchasePriceEntity.getDetailList ().forEach ( detail -> {
                detail.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                detail.setPriceInfoId ( kingPurchasePriceEntity.getId () );
                detail.setUserId ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
                detail.setCreateTime ( DateUtils.getNowDate () );
                kingPurchasePriceDetailService.save ( detail );
            } );
        }
        return kingPurchasePriceMapper.updateKingPurchasePriceEntity ( kingPurchasePriceEntity );
    }

    /**
     * 批量删除金蝶-采购价目/折扣主表
     *
     * @param ids 需要删除的金蝶-采购价目/折扣主表ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingPurchasePriceEntityByIds ( String[] ids ) {
        boolean update = this.update ( new LambdaUpdateWrapper<KingPurchasePriceEntity> ().
                set ( KingPurchasePriceEntity :: getChangeTime, DateUtils.getDate () ).
                set ( KingPurchasePriceEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                in ( KingPurchasePriceEntity :: getId, ids ) );
        if ( ! update ) {
            throw new CustomException ( "删除价目表失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 删除金蝶-采购价目/折扣主表信息
     *
     * @param id 金蝶-采购价目/折扣主表ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled ( String id ) {
        KingPurchasePriceEntity purchasePriceEntity = this.getById ( id );
        if ( purchasePriceEntity == null ) {
            throw new CustomException ( "无此价目表！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingPurchasePriceEntity> ().
                set ( KingPurchasePriceEntity :: getChangeTime, DateUtils.getDate () ).
                set ( KingPurchasePriceEntity :: getIsDisabled, purchasePriceEntity.getIsDisabled () == 1 ? 2 : 1 ).
                eq ( KingPurchasePriceEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( purchasePriceEntity.getIsDisabled () == 1 ? "禁用价目表失败！" : "解禁价目表失败！" );
        }
        return AjaxResult.success ();
    }
}
